﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using AutoMapper;
using CNav.Business.Services;
using CNav.Common;
using CNav.Domain;
using CNav.Model;

namespace CNav.DataApi.DataApi
{
    public class ConsumerController : BaseApiController
    {
        [Authorize, HttpPost]
        [ActionName("Search")]
        public IList<ConsumerModel> Search(SearchModel searchModel)
        {
            Argument.IsNotNull("searchType", searchModel.SearchType);
          
            ResponseData<List<Consumer>> consumerList = null;

            switch (searchModel.SearchType.ToUpper())
            {
                case "FIRSTNAME":
                    consumerList = Service.Get<ISearchService>().GetConsumerByFirstName(searchModel.SearchText);
                    break;
                case "LASTNAME":
                    consumerList = Service.Get<ISearchService>().GetConsumerByLastName(searchModel.SearchText);
                    break;
                case "BIRTHDATE":
                    consumerList = Service.Get<ISearchService>().GetConsumerByBirthDate(searchModel.SearchText);
                    break;
                case "EMAIL":
                    consumerList = Service.Get<ISearchService>().GetConsumerByEmail(searchModel.SearchText);
                    break;
                case "PHONE":
                    consumerList = Service.Get<ISearchService>().GetConsumerByPhone(searchModel.SearchText);
                    break;
                case "POSTCODE":
                    consumerList = Service.Get<ISearchService>().GetConsumerByPostCode(searchModel.SearchText);
                    break;
            }

            IList<ConsumerModel> consumerModelList = ToConsumerModelList(consumerList);

            return consumerModelList;
        }

        [Authorize, HttpPost]
        [ActionName("Search")]
        public ConsumerModel Get(int id)
        {
           var response =  Service.Get<ISearchService>().GetConsumerByID(id);
            if (response.Data!= null)
            {
                return Mapper.Map<ConsumerModel>(response.Data);
            }

            return null;
        }

        private IList<ConsumerModel> ToConsumerModelList(ResponseData<List<Consumer>> consumerList)
        {
            return consumerList.Data.Select(Mapper.Map<ConsumerModel>).ToList();
        }
    }
}